package com.aiti.lulian.service.impl;

import cn.hutool.core.util.IdUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.constant.NewCardBusinessConstant;
import com.aiti.lulian.dto.CardUserCardForwardCoverOptionDto;
import com.aiti.lulian.entity.CardUserCardForwardCoverOption;
import com.aiti.lulian.mapper.CardUserCardForwardCoverOptionMapper;
import com.aiti.lulian.mapper.newCard.CardDefaultTemplateMapper;
import com.aiti.lulian.service.CardUserCardForwardCoverOptionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 个人名片转发封面选择表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-08-11
 */
@Service
public class CardUserCardForwardCoverOptionServiceImpl extends BaseSerivceImpl<CardUserCardForwardCoverOptionMapper, CardUserCardForwardCoverOption> implements CardUserCardForwardCoverOptionService {

    @Autowired
    private CardDefaultTemplateMapper cardDefaultTemplateMapper;

    @Override
    public Message savePre(CardUserCardForwardCoverOption entity) {
        entity.setCustomFlag("0");
        this.save(entity);
        return Message.success();
    }

    @Override
    public Message updatePre(CardUserCardForwardCoverOption entity) {
        String baseId = entity.getBaseId();
        if(StringUtils.isEmpty(baseId)){
            return Message.fail("传参错误");
        }

        this.updateById(entity);
        return Message.success();
    }

    @Override
    public Message coverPage(Page page, CardUserCardForwardCoverOptionDto dto) {

        LambdaQueryWrapper<CardUserCardForwardCoverOption> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CardUserCardForwardCoverOption::getVersion, dto.getVersion()).eq(CardUserCardForwardCoverOption::getType, dto.getType()).eq(!StringUtils.isEmpty(dto.getTemplateType()), CardUserCardForwardCoverOption::getTemplateType, dto.getTemplateType())
                .eq(CardUserCardForwardCoverOption::getCustomFlag, "0");

        Page<CardUserCardForwardCoverOption> page1 = this.page(page, wrapper);
        PageResult rspData = new PageResult(
                page1.getRecords(),
                new PageInfo(dto.getPageNum(), dto.getPageSize()),
                page1.getTotal()
        );
        return Message.success(rspData);
    }

    @Override
    public Message delete(String baseId) {
        this.removeById(baseId);
        return Message.success();
    }

    public void createDefaultForwardCover(String userId, String cardId, String coverUrl, String coverUrlType, String coverUrlBaseId) {
        CardUserCardForwardCoverOption coverOption = new CardUserCardForwardCoverOption();
        coverOption.setBaseId(IdUtil.simpleUUID());
        coverOption.setUrl(coverUrl);
        coverOption.setUserId(userId);
        coverOption.setCardId(cardId);
        coverOption.setCustomFlag("1");
        coverOption.setVersion("2");
        coverOption.setTemplateType("0");
        coverOption.setCreator(userId);
        coverOption.setUpdateBy(userId);
        coverOption.setHelloWordType(0);
        coverOption.setHelloWord(cardDefaultTemplateMapper.queryDefaultHelloWord());
        coverOption.setType(coverUrlType);
        coverOption.setCoverUrlBaseId(coverUrlBaseId);
        this.save(coverOption);
    }
}
